package lovo.practice_7_14.util;

import lovo.practice_7_14.pojo.UserDO;

import java.sql.*;

public class DBUtil {
    private Connection con;
    private PreparedStatement statement;

    public void setAutoCommit(boolean autoCommit) {
        try {
            con.setAutoCommit(autoCommit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void commit() {
        try {
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void rollback(){
        try {
            con.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void close() {

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public Connection getConnection() {
//        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "0201");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public void close(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据用户名和密码查询用户对象
     *
     * @param username
     * @param pwd
     * @return
     */
//    public UserDO getByUsernameAndPwd(String username,String pwd){
//        Connection con = getConnection();
//        Statement statement = null;
//        ResultSet rs = null;
//        UserDO user = null;
//        String sql = "select * from t_atm where a_account='"+username+"' and a_password='"+pwd+"'";
//        try {
//            statement = con.createStatement();
//            rs = statement.executeQuery(sql);
//            if(rs.next()){
//                user = new UserDO();
////                user.set(rs.getString("u_account"));
//                user.setAccount(rs.getString("a_account"));
//                user.setPwd(rs.getString("a_password"));
//                user.setBalance(rs.getDouble("a_balance"));
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            close(con,statement,rs);
//        }
//        return user;
//    }
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO userDO = null;
//        String sql = "select * from t_atm where a_account='"+username+"' and a_password='"+pwd+"'";
        String sql = "select  * from t_atm where a_account =? and a_password =? ";
        try {
            statement = con.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
                userDO.setAccount(rs.getString("a_account"));
                userDO.setPwd(rs.getString("a_password"));
                userDO.setBalance(rs.getDouble("a_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userDO;
    }

    /**
     * 根据账号名来查询 返回一个user类
     *
     * @param
     * @return
     */
//    public UserDO getById(String account) {
//        Connection con = getConnection();
//        Statement statement = null;
//        ResultSet rs = null;
//        UserDO user = null;
//        String sql = "select * from t_atm where a_account= '" + account + "'";
//        try {
//            statement = con.createStatement();
//            rs = statement.executeQuery(sql);
//            if (rs.next()) {
//                user = new UserDO();
//                user.setAccount(rs.getString("a_account"));
//                user.setPwd(rs.getString("a_password"));
//                user.setBalance(rs.getDouble("a_balance"));
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            close(con, statement, rs);
//        }
//        return user;
//    }
    public UserDO getById(String account) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO userDO = null;
        String sql = "select  * from t_atm where a_account =?";
        try {
            statement = con.prepareStatement(sql);
            statement.setString(1, account);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
                userDO.setAccount(rs.getString("a_account"));
                userDO.setPwd(rs.getString("a_password"));
                userDO.setBalance(rs.getDouble("a_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userDO;
    }

    /**
     * 根据User对象中的数据进行修改
     *
     * @param userDO
     */
//    public void update(UserDO userDO) {
//        // 1. 创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//        try {
//            // 2. 获取执行SQL语句对象
//            statement = con.createStatement();
//            String sql = "update t_atm set a_account='" + userDO.getAccount() + "',a_balance=" + userDO.getBalance() + ",a_password='" + userDO.getPwd() + "' where a_account='" + userDO.getAccount() + "'";
//
//            // 3. 执行SQL语句
//            int row = statement.executeUpdate(sql);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            // 4. 关闭连接
//            close(con, statement);
//        }
//    }
    //普通的更新数据
    public void update(UserDO userDO) {
        // 1. 创建连接
        Connection con = getConnection();
        PreparedStatement statement = null;
        try {
            // 2. 获取执行SQL语句对象
            String sql = "update t_atm set a_account=?,a_password=?,a_balance=? where a_account =?";

            statement = con.prepareStatement(sql);
            statement.setString(1, userDO.getAccount());
            statement.setString(2, userDO.getPwd());
            statement.setDouble(3, userDO.getBalance());
            statement.setString(4, userDO.getAccount());
            int row = statement.executeUpdate();
            System.out.println(row);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con, statement);
        }
    }

    /**
     * 事务转账
     */
    public void update1(UserDO userDO) {
//        // 1. 创建连接
//        Connection con = getConnection();
//        PreparedStatement statement = null;
        String sql = "update t_atm set a_account=?,a_password=?,a_balance=? where a_account =?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setString(1, userDO.getAccount());
            statement.setString(2, userDO.getPwd());
            statement.setDouble(3, userDO.getBalance());
            statement.setString(4, userDO.getAccount());
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
        }
    }


}
